package com.example.controller;

import com.example.pojo.*;
import com.example.service.ActivityService;
import com.example.service.ClassService;
import com.example.service.StudentService;
import com.example.utils.Result;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@Api(value = "教学班管理接口")
public class ClassController {
    @Autowired
    private ClassService classService;
    @Autowired
    private ActivityService activityService;
    @Autowired
    public StudentService studentService;

    //创建
    @PostMapping("/class")
    public Result createClass(@RequestBody TeachingClass teachingClass){
        log.info("创建班级");
        int id = 1;
        //查询id为1的活动
        Activity activity = activityService.query(id);
        String status = activity.getStatus();
        //判断是否发布
        try{
            if(status.equals("1")){
                classService.createClass(teachingClass);
            }
        }catch (Exception e) {
            return Result.error("活动未发布，无法关联");
        }
        return Result.success("教学班创建成功");

    }

    //编辑
    @PutMapping("/class")
    public Result updateClass(@RequestBody TeachingClass teachingClass){
        log.info("修改教学班信息");
        classService.updateClass(teachingClass);
        return Result.success();
    }

    //查询
    @GetMapping("/class")
    public Result queryClass(@RequestParam String name){
        log.info("查询{}的教学班"+name);
        TeachingClass teachingClass = classService.query(name);
        return Result.success(teachingClass);
    }

    //分配教师
    @PostMapping("/class/assign")
    public Result assignTeacher(@RequestParam Integer id,String name){
        log.info("分配教师");
        classService.assignTeacher(id,name);
        return Result.success();
    }

    //生成小组
    @PostMapping("class/group")
    public Result createGroup(@RequestBody Group group){
        log.info("生成小组");
        classService.createGroup(group);
        return Result.success();
    }

    //关联学生
    @PostMapping("class/student")
    public Result connectStudent(@RequestBody TeachingStudentInClass teachingStudentInClass){
        log.info("关联学生");
        int id = 5;
        try{
            Student student = studentService.query(id);
            if(student.getStatus().equals("已审核")){
                classService.connectStudent(teachingStudentInClass);
            }

        }catch (Exception e){
            return Result.error("学员未审核，关联失败");
        }
        return Result.success("关联成功");

    }



}
